<template>
  <Modal v-model="modal" title="请选择需要将人员移动至的组织" :closable="false" :mask-closable="false" :width='500'>
    <template v-slot:footer>
      <div>
        <div style="flex: 1;" />
        <Button @click='onCancel'>取消</Button>
        <Button @click='onOk' :disabled='disabled' type="primary">确定</Button>
      </div>
    </template>

    <Tree :data="orgs" @on-select-change="onSelect"></Tree>
    
    <confirm-modal ref='confirm' @success='onSuccess' />
  </Modal>
</template>

<script>
  import confirmModal from './confirm-modal.vue'
  import toolApi from '@/biz/base/tool'
  import orgOpt from '@/biz/option/organization'

  export default {
    components: {
      confirmModal
    },
    data() {
      return {
        modal: false,
        disabled: true,
        info: {
          toOrgId: null,
          userIds: null
        },
        orgs: []
      }
    },
    methods: {
      onOk() {
        this.$refs.confirm.show(this.info)
      },
      onSuccess() {
        this.onCancel()
        this.$emit('success')
      },
      show(info) {
        this.info = info
        this.modal = true
        this.getBusinessList()
      },
      onCancel() {
        this.info = {
          toOrgId: null,
          userIds: null
        }
        this.disabled = true
        this.modal = false
      },
      onSelect(event) {
        if (!event.length || event[0].level == 0 || event[0].level == 1) {
          this.info.toOrgId = null
          this.disabled = true
        } else {
          this.info.toOrgId = event[0]
          this.disabled = false
        }
      },
      getBusinessList() {
        toolApi.organization().then((res) => {
          if (res && res.length) {
            res = [{
              level: 0,
              orgName: '同策',
              children: res
            }]
            this.recursion(res)
          }
          this.orgs = res || []
        })
      },
      recursion(data, parent) {
        data.forEach(item => {
          item.expand = 'true'
          item.title = item.orgName
          if (parent) {
            item.pOrgId = parent.orgId
            item.pOrgName = parent.orgName
          }
          if (item.children && item.children.length) {
            this.recursion(item.children, item)
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  ::v-deep .ivu-modal-body {
    max-height: 500px;
    overflow-y: auto;
  }
</style>
